//
// Created by gogoqiu on 2022/3/23.
//

#ifndef RAPIDSVN_CMAKE_SQLITEQUERYENGINE_H
#define RAPIDSVN_CMAKE_SQLITEQUERYENGINE_H

#include "base.h"
#include <wx/wx.h>
#include <utility>
#include <vector>
#include <sqlite3.h>
#include "BaseItem.h"

#define VALUE_MAX_LENGTH 255

//MySettingsQueryEngineInterface
class SqliteQueryEngine :public MySettingsQueryEngineInterface{
public:
    explicit SqliteQueryEngine( const wxString &AppId );
    ~SqliteQueryEngine();
    int QueryString( char* key, char* result ) override;
    int QueryBoolean( char* key, bool * result ) override;
    int InitTable();
    //
    int QueryItem( wxString& key, BaseItem* item );
    //get current items
    int FillItems( wxVector<BaseItem*>* items );
    //raw items
    int Setup( wxVector<BaseItem*>* items );

    //if not exist, create
    int UpdateItem( const wxString& key, const wxString& value );

private:
    sqlite3* m_pDb;
    wxString appId;
    int QueryValue( char* key, char* result );
    //wxString AppId;
    //
    int RunSql( const wxString& sql );

    //
    BaseItem * Row2Item( char **dbResult, int nColumn, int rowIndex );
    int Row2Item( char **dbResult, int nColumn, int rowIndex, BaseItem * item );
    //
    int getRecordCount( const wxString& key );
    int SetupItem( BaseItem* item );
};

#endif //RAPIDSVN_CMAKE_SQLITEQUERYENGINE_H
